package org.jwxa.jwxa4j.parser;

import org.jsoup.nodes.Element;
import org.jwxa.jwxa4j.nodes.WXNode;
import org.jwxa.jwxa4j.nodes.WXRoot;

/**
 * 默认Tag标签过滤器
 * @author janjan, xujian_jason@163.com
 *
 */
public class DefaultTagFilter implements TreeFilter {
	
	@Override
	public void doFilter(WXRoot root, WXNode currentNode, Object object) {
		if (object instanceof Element) {
			Element element = (Element) object;
			String tagName = element.tagName();
			if("img".equalsIgnoreCase(tagName)) {
				imgFilter(root, currentNode, element);
			}
			StringBuilder nodeTypeBuilder = new StringBuilder();
			nodeTypeBuilder.append(",").append("button").append(",");
			nodeTypeBuilder.append(",").append("a").append(",");
			nodeTypeBuilder.append(",").append("audio").append(",");
			nodeTypeBuilder.append(",").append("img").append(",");
			nodeTypeBuilder.append(",").append("video").append(",");
			if(nodeTypeBuilder.indexOf(","+tagName+",") > -1) {
				currentNode.setNodeType(tagName);
			} else {
				currentNode.setNodeType("view");
			}
		}
	}
	
	// img标签统计
	private void imgFilter(WXRoot root, WXNode currentNode, Element element) {
		root.addImageUrl(element.attributes().get("src"));
	}

}
